﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Windows.Media.Imaging;

namespace MushroomPicker
{
    public enum MushroomState
    {
        None,Showing,Check,Error,Hide
    }
    
	public partial class Mushroom : UserControl
	{
        private static List<BitmapImage> bitmaps = new List<BitmapImage>()
        {
            new BitmapImage(new Uri("/MushroomPicker;component/Res/0.png", UriKind.Relative)),
            new BitmapImage(new Uri("/MushroomPicker;component/Res/1.png", UriKind.Relative)),
            new BitmapImage(new Uri("/MushroomPicker;component/Res/2.png", UriKind.Relative)),
            new BitmapImage(new Uri("/MushroomPicker;component/Res/3.png", UriKind.Relative)),
            new BitmapImage(new Uri("/MushroomPicker;component/Res/4.png", UriKind.Relative)),
            new BitmapImage(new Uri("/MushroomPicker;component/Res/5.png", UriKind.Relative)),
        };
		public Mushroom()
		{
			// Required to initialize variables
			InitializeComponent();
            ANI_Close.Completed += new EventHandler(ANI_Close_Completed);
            ANI_Hide.Completed += new EventHandler(ANI_Hide_Completed);
            ANI_Show.Completed += new EventHandler(ANI_Show_Completed);
            
		}

        void ANI_Show_Completed(object sender, EventArgs e)
        {
            _MushroomState = MushroomPicker.MushroomState.None;
            if (NextMushroom != null)
                NextMushroom.MushroomState = MushroomPicker.MushroomState.Showing;
            else
                if (AllShowed != null)
                    AllShowed();
        }

        void ANI_Hide_Completed(object sender, EventArgs e)
        {
            if (NextMushroom != null)
                NextMushroom.MushroomState = MushroomPicker.MushroomState.Hide;
            else
                if (AllHided != null)
                    AllHided();
            //this.Visibility = System.Windows.Visibility.Collapsed;
        }
        public void Show()
        {
            ANI_Show.Begin();
            this.Visibility = System.Windows.Visibility.Visible;
        }
        public Action AllHided { get; set; }
        public Action AllShowed { get; set; }
        public Action AllClosed { get; set; }
        void ANI_Close_Completed(object sender, EventArgs e)
        {
            if (this.NextMushroom != null)
                NextMushroom.MushroomState = MushroomPicker.MushroomState.Error;
            else
                if (AllClosed != null)
                    AllClosed();
            this.Visibility = System.Windows.Visibility.Collapsed;
        }
        public Mushroom PrevMushroom = null;
        public Mushroom NextMushroom = null;
        private MushroomState _MushroomState;
        public MushroomState MushroomState
        {
            get { return _MushroomState; }
            set
            {
                if (_MushroomState == value || _MushroomState == MushroomPicker.MushroomState.Showing)
                    return;
                image2.Visibility = System.Windows.Visibility.Collapsed;
                switch (value)
                {
                    case MushroomState.None:
                        break;
                    case MushroomState.Showing:
                        Show();
                        break;
                    case MushroomState.Check:
                        Sound.PlayClick();
                        image2.Visibility = System.Windows.Visibility.Visible;
                        break;
                    case MushroomState.Error:
                        Sound.PlayWrong();
                        ANI_Close.Begin();
                        break;
                    case MushroomState.Hide:
                        image4.Source = bitmaps[Game.Random.Next() % bitmaps.Count];
                        ANI_Hide.Begin();
                        break;
                    default:
                        break;
                }
                _MushroomState = value;
            }

        }
        public double X
        {
            get { return Canvas.GetLeft(this); }
            set
            {
                Canvas.SetLeft(this, value);
            }
        }
        public double Y
        {
            get { return Canvas.GetTop(this); }
            set
            {
                Canvas.SetTop(this, value);
            }
        }
        public Rect Rect
        {
            get { return new Rect(X, Y, this.Width, this.Height); }
        }
        Point SavePoint;
        public void SavePos()
        {
            SavePoint = new Point(X, Y);
        }
        public void ReSetPos()
        {
            X = SavePoint.X;
            Y = SavePoint.Y;
        }
	}
}